// ActionScript file
package
{
      import flash.events.Event;
      import flash.events.MouseEvent;
      
      import mx.core.Application;
      import mx.core.IMXMLObject;
      import mx.managers.PopUpManager;
      import mx.messaging.Consumer;
      import mx.messaging.Producer;
      import mx.messaging.messages.AsyncMessage;
      import mx.messaging.messages.IMessage;
      
	class StickyNoteLogic implements IMXMLObject{
      
      private static const OP_TEXTBOX:String = "textbox";
      private const SIZE_DRAGAREA:int = 5;
      private const SIZE_MIN_WIDTH:int = 40;
      private const SIZE_MIN_HEIGHT:int = 30;

      private var blDragTop:Boolean = false;
      private var blDragRight:Boolean = false;
      private var blDragBottom:Boolean = false;
      private var blDragLeft:Boolean = false;
      private var iDragPosHeight:int = 0;
      private var iDragPosWidth:int = 0;
       

      private var arrayMyTextBox:Array = [];
      private var producer:Producer;
      private var consumer:Consumer;
      private var numberTB:Number;
      private var view:StickyNote;
           
      public function initialized(document:Object, id:String):void 
      {   
      	view = document as StickyNote;  
      }
      
      
      public function setProducer(parentProducer:Producer):void
      {
      	producer = parentProducer;
      }
      
      public function setConsumer(parentConsumer:Consumer):void
      {
      	consumer = parentConsumer;
      }
      
      public function setNumberTextBox(num:Number):void
      {      	
      	numberTB = new Number(num);
      }
      
      public function getNumberTB():Number
      {
      	return numberTB;
      }
      
      public function setTitle(title:String):void
      {
      		view.title = title;
      }
      
      public function getView():Object
      {
      	return view;
      }
      
      public function onThis_creationComplete():void
      {
        // 親画面にmouseUpとmouseMoveイベントを追加
        Application.application.parent.addEventListener(MouseEvent.MOUSE_UP, onParent_mouseUp);
        Application.application.parent.addEventListener(MouseEvent.MOUSE_MOVE, onParent_mouseMove);      
      }

      public function onThis_mouseDown(event:MouseEvent):void
      {
        // check Top pos
        if( event.localY < SIZE_DRAGAREA )
        {
          blDragTop = true;
          iDragPosHeight = event.localY;
        }

        // check Right pos
        if( view.width - SIZE_DRAGAREA < event.localX )
        {
          blDragRight = true;
          iDragPosWidth = view.width - event.localX;
        }

        // check Bottom pos
        if( view.height - SIZE_DRAGAREA < event.localY )
        {
          blDragBottom = true;
          iDragPosHeight = view.height - event.localY;
        }
        
        // check Left pos
        if( event.localX < SIZE_DRAGAREA )
        {
          blDragLeft = true;
          iDragPosWidth = event.localX;
        }
      }

      public function onParent_mouseMove(event:MouseEvent):void
      {
        doDrag(event.stageX, event.stageY);
      }

      public function onThis_mouseMove(event:MouseEvent):void
      {
        doDrag(event.stageX, event.stageY);
      }

      public function onParent_mouseUp(event:MouseEvent):void
      {
        outDragMode();
      }

      public function onThis_mouseUp(event:MouseEvent):void
      {
        outDragMode();
      }
      
      public function onThis_close():void
      {
        PopUpManager.removePopUp(view);
      }

      private function doDrag(intX:int, intY:int):void
      {
        if( blDragTop )
        {
          if( view.y + view.height - intY + iDragPosHeight > SIZE_MIN_HEIGHT )
          {
            view.height = view.y + view.height - intY + iDragPosHeight;
            view.y = intY - iDragPosHeight;
          }
          else
          {
            view.y = view.y + view.height - SIZE_MIN_HEIGHT;
            view.height = SIZE_MIN_HEIGHT;
          }
        }

        if( blDragRight )
        {
          if( intX - view.x + iDragPosWidth > SIZE_MIN_WIDTH )
          {
            view.width = intX - view.x + iDragPosWidth;
          }
          else
          {
            view.width = SIZE_MIN_WIDTH;
          }
        }
        
        if( blDragBottom )
        {
          if( intY - view.y + iDragPosHeight > SIZE_MIN_HEIGHT )
          {
            view.height = intY - view.y + iDragPosHeight;
          }
          else
          {
            view.height = SIZE_MIN_HEIGHT;
          }
        }
        
        if( blDragLeft )
        {
          if( view.x + view.width - intX + iDragPosWidth > SIZE_MIN_WIDTH )
          {
            view.width = view.x + view.width - intX + iDragPosWidth;
            view.x = intX - iDragPosWidth;
          }
          else
          {
            view.x = view.x + view.width - SIZE_MIN_WIDTH;
            view.width = SIZE_MIN_WIDTH;
          }
        }

        //位置・大きさ・テキスト・固有番号をセット
        if (consumer.subscribed )
        {
        	arrayMyTextBox.push(view.title, 
        						numberTB,
        						view.myTextBox.text,
        						view.x, 
        						view.y, 
        						view.width, 
        						view.height);
    		var msg:IMessage = new AsyncMessage();
	    	msg.body = arrayMyTextBox;
	    	msg.headers["operation"] = OP_TEXTBOX;
	    	producer.send(msg);
        }
        
      }
      
      private function outDragMode():void
      {
        blDragTop = blDragRight = blDragBottom = blDragLeft = false;
      }
      
      public function onThis_Change(event:Event):void
      {
      	if (consumer.subscribed )
      	{
	      	//位置・大きさ・テキストをセット
	        arrayMyTextBox.push(view.title, 
        						numberTB,
        						view.myTextBox.text,
        						view.x, 
        						view.y, 
        						view.width, 
        						view.height);
	    	var msg:IMessage = new AsyncMessage();
		    msg.body = arrayMyTextBox;
		    msg.headers["operation"] = OP_TEXTBOX;
		    producer.send(msg);
      	}
      }
      
	}   
      
}